package com.mlan.userap.okhttp;

import com.mlan.userap.okhttp.model.LoginResponse;

import java.util.HashMap;
import java.util.Map;

public class RequestBuilder {
    private String baseUrl;
    private Map<String, String> params;
    private String token;

    public RequestBuilder(String baseUrl) {
        this.baseUrl = baseUrl;
        this.params = new HashMap<>();
    }

    /**
     * 添加键值对参数
     * @param key 参数名
     * @param value 参数值
     * @return 当前 RequestBuilder 实例，支持链式调用
     */
    public RequestBuilder addPara(String key, String value) {
        params.put(key, value);
        return this;
    }

    /**
     * 设置认证 Token
     * @param token 认证 Token
     * @return 当前 RequestBuilder 实例
     */
    public RequestBuilder setToken(String token) {
        this.token = token;
        return this;
    }

    /**
     * 发起 GET 请求
     * @param callback 泛型回调函数
     * @param <T> 返回的实体类型
     */
    public <T> void getData(JsonCallBackData<T> callback) {
        OkHttpHelper.getData(buildUrl(), callback);
    }
    public <T> void get(JsonCallBack<T> callback) {
        OkHttpHelper.get(buildUrl(), callback);
    }
    private String buildUrl() {
        StringBuilder urlBuilder = new StringBuilder(baseUrl);
        if (!params.isEmpty()) {
            urlBuilder.append("?");
            boolean first = true;
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (!first) urlBuilder.append("&");
                urlBuilder.append(entry.getKey()).append("=").append(entry.getValue());
                first = false;
            }
        }
        return urlBuilder.toString();
    }
}